package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import com.mbc.common.CommonConstants.RecordStatus;
import com.mbc.common.entities.Branch;

public class IAIMSBranchDAOImpls extends DAO<Object> implements IAIMSBranchDAO {
	
	public static final String field_office_id = "office_id";
	public static final String field_parent_id = "parent_id";
	public static final String field_office_type_id = "office_type_id";
	public static final String field_office_code = "office_code";
	public static final String field_name = "name";
	public static final String field_myanmar_name = "myanmar_name";
	public static final String field_phone = "phone";
	public static final String field_fax = "fax";
	public static final String field_email = "email";
	public static final String field_address = "address";
	public static final String field_created_date = "created_date";
	public static final String field_record_status = "record_status";
	
	public static final int data_add = 1;
	public static final int data_update = 2;

	public IAIMSBranchDAOImpls(Connection conn){
		this.conn = conn;
		this.table_name = "Office";
	}
	
	@Override
	public HashMap<String, Long> getBranchIdNCodes() throws SQLException {
		HashMap<String, Long> l_map = new HashMap<String, Long>();
		query = "select Office_Id, Code from " + table_name;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while(rs.next()){
			l_map.put(rs.getString("Code"), rs.getLong("Office_Id"));
		}
		rs.close();
		pstmt.close();
		return l_map;
	}

	@Override
	protected Object readRecord(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void create(Branch branch) throws SQLException {
		//query = "insert into " + table_name + " ( office_id, parent_id, office_type_id,office_code, name, myanmar_name, phone, fax, email, address, created_date, record_status) values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		query = insertParam(table_name, new String[] { field_office_id, field_parent_id, field_office_type_id,
				field_office_code, field_name, field_myanmar_name, field_phone, field_fax, field_email,
				field_address, field_created_date, field_record_status });
		pstmt = conn.prepareStatement(query);
		updateRecord(branch, data_add);
		pstmt.executeUpdate();
		pstmt.close();
	}
	
	protected void updateRecord(Branch entity, int mode) throws SQLException {
		SimpleDateFormat l_dateFormat = new SimpleDateFormat("yyyyMMdd");
		
		int i = 1;
		pstmt.setLong(i++, entity.getBranchId());
		pstmt.setLong(i++, 0);
		pstmt.setLong(i++, 0);
		pstmt.setString(i++, entity.getBranchCode());
		pstmt.setString(i++, entity.getName());
		pstmt.setString(i++, entity.getMyanmarName());
		pstmt.setString(i++, entity.getPhone());
		pstmt.setString(i++, entity.getFax());
		pstmt.setString(i++, entity.getEmail());
		pstmt.setString(i++, entity.getAddress());
		if(mode == data_add)
			pstmt.setString(i++, l_dateFormat.format(entity.getCreatedDate()));
		else if(mode == data_update)
			pstmt.setString(i++, l_dateFormat.format(entity.getModifiedDate()));
		pstmt.setByte(i++, RecordStatus.active);
	}

	@Override
	public long getKeyByCode(String code) throws SQLException {
		long l_key = 0;
		query = "select Office_id from " + table_name + " where code = '" + code + "'";
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next())
			l_key = rs.getLong(1);
		rs.close();
		pstmt.close();
		return l_key;
	}

	@Override
	public void update(Branch entity) throws SQLException {
		query = updateParam(table_name, new String[] { field_office_id, field_parent_id, field_office_type_id,
				field_office_code, field_name, field_myanmar_name, field_phone, field_fax, field_email,
				field_address, field_created_date, field_record_status }) + " AND " + field_office_id + " = " + entity.getBranchId();
		pstmt = conn.prepareStatement(query);
		updateRecord(entity, data_update);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where " + field_office_id + " = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

}
